﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Dude.MLS;
using System.ComponentModel;
using System.Data.Common;
using Dude.Data;

namespace Dude.Data.Meta
{

    [Serializable]
    [XmlRoot(ElementName = "connection")]
    public class DatabaseConnectionInfo : IVisitedDataModel
    {
        public DatabaseConnectionInfo()
        {
            Description = new MLString();
            LongDescription = new MLString();
        }

        [Category("General")]
        [XmlAttribute("name")]
        public string ConnectionName { get; set; }

        [Category("General")]
        [XmlElement("providerclass")]
        public string ProviderInvariantName { get; set; }

        [Category("General")]
        [XmlElement("providerassembly")]
        public string ProviderAssembly { get; set; }

        [Category("General")]
        [XmlElement("connectionstring")]
        public string ConnectionString { get; set; }

        [Category("General")]
        [XmlElement("alias")]
        public string[] Aliases { get; set; }

        [Category("Localization")]
        [XmlElement("desc")]
        public MLString Description { get; set; }

        [Category("Localization")]
        [XmlElement("longdesc")]
        public MLString LongDescription { get; set; }

        private DbProviderFactory dbProviderFactory = null;
        public DbProviderFactory GetDbProviderFactory()
        {
            return dbProviderFactory ?? (dbProviderFactory = Providers.GetDbProviderFactory(ProviderInvariantName, ProviderAssembly));
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder(ConnectionName);
            foreach (string alias in Aliases)
            {
                sb.Append(", ");
                sb.Append(alias);
            }
            return sb.ToString();
        }


        #region IVisitedDataModel Members

        public void VisitedDataModel(IDataModelVisitor visitor)
        {
            visitor.VisitDatabaseConnectionInfo(this);
        }

        #endregion
    }
}
